package com.android.porttallymansystem.ui.activity

import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Intent
import android.content.IntentFilter
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import com.amap.api.location.AMapLocationClient
import com.android.porttallymansystem.PTSApplication
import com.android.porttallymansystem.R
import com.android.porttallymansystem.databinding.ActivityMainBinding
import com.android.porttallymansystem.logic.receiver.AlarmReceiver
import com.android.porttallymansystem.logic.service.LocationService
import com.android.porttallymansystem.ui.fragment.MapFragment
import com.android.porttallymansystem.ui.fragment.MessageFragment
import com.android.porttallymansystem.ui.fragment.SelfFragment
import com.android.porttallymansystem.ui.fragment.WorkFragment

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    private var mFragments = mutableListOf<Fragment>()

    private lateinit var alarmReceiver: AlarmReceiver

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        hideSystemBars()

        initData()

        setListener()

        mapPrivacy()

        startLocationService()

        val intentFilter = IntentFilter()
        intentFilter.addAction("LOCATION_CLOCK")
        alarmReceiver = AlarmReceiver()
        registerReceiver(alarmReceiver, intentFilter)

        startAlarm()
    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(alarmReceiver)
    }

    private fun startLocationService() {
        startService(Intent(this, LocationService::class.java))
    }

    private fun startAlarm() {
        val intent = Intent("LOCATION_CLOCK")
        val pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0)
        val alarmManager = getSystemService(ALARM_SERVICE) as AlarmManager;
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 15* 1000, pendingIntent)
    }

    // 在构造AMapLocationClient 之前必须进行合规检查，设置接口之前保证隐私政策合规
    private fun mapPrivacy() {
        AMapLocationClient.updatePrivacyShow(PTSApplication.context.applicationContext, true, true)
        AMapLocationClient.updatePrivacyAgree(PTSApplication.context.applicationContext, true)
    }

    // 给控件添加监听器
    private fun setListener() {
        binding.bottomNavigationView.setOnNavigationItemSelectedListener { item ->
            when (item.itemId) {
                R.id.menu_work -> setFragmentPosition(0)
                R.id.menu_map -> setFragmentPosition(1)
                R.id.menu_message -> setFragmentPosition(2)
                R.id.menu_self -> setFragmentPosition(3)
            }
            true
        }
    }

    // 状态栏适配
    private fun hideSystemBars() {
        val decorView = window.decorView
        // 状态栏调整为黑色字体，透明底色
        decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        window.statusBarColor = resources.getColor(R.color.light_gray)
    }

    // 初始化数据
    private fun initData() {
        mFragments.add(WorkFragment())
        mFragments.add(MapFragment())
        mFragments.add(MessageFragment())
        mFragments.add(SelfFragment())
        setFragmentPosition(0)
    }

    // 切换Fragment
    private fun setFragmentPosition(position: Int) {
        val ft = supportFragmentManager.beginTransaction()
        ft.replace(R.id.fragmentLayout, mFragments[position]).commit()
    }
}